package com.x.Part2_twoDimensionArray;
/**
 * ClassName: TwoDimensionArray1
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2023/9/12 17:48
 */
public class TwoDimensionArray1 {
    public static void main(String[] args) {

        //静态初始化:数组变量(arr1)赋值和元素赋值同时进行
        int[][] arr1 = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        String[][] arr2 = new String[][]{{"123", "456"},{"678"}};

        //动态初始化:数组变量和元素的赋值分开进行
        String[][] arr3 = new String[2][3];
        //错误写法:无法进行类型推断
        // arr3[0] = {"123", "123", "123"};
        arr3[0] = new String[]{"123", "456", "789"};
        arr3[1] = new String[]{"!@#", "##"};

        System.out.println(arr3[0].length);//3
        System.out.println(arr3[1][1].length());//1

        //动态初始化方式2
        int[][] arr6 = new int[3][];
        //外层元素默认值
        System.out.println(arr6[0]);//输出null
        //内层元素默认值(报错)
        //System.out.println(arr6[0][0]);//报错NullPointerException,内层元素不存在

        //数组遍历
        for (int i = 0; i < arr3.length; i++){
            for (int j = 0; j < arr3[i].length; j++) {
                System.out.print(arr3[i][j]);
                System.out.print('\t');
            }
            System.out.println();
        }

        //查看外层元素和内层元素的默认初始值
        int[][] arr4 = new int[3][2];
        System.out.println(arr4[0]);//输出arr[0]地址值
        System.out.println(arr4[1]);//输出arr[1]地址值
        System.out.println(arr4[0][0]);//输出0

        boolean[][] arr5 = new boolean[3][2];
        System.out.println(arr5[0]);//仍然是地址值
        System.out.println(arr5[0][0]);//默认为false




    }
}
